package com.yj.suncity.utis;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.yj.suncity.R;

@SuppressLint("ViewHolder")
public class DialogUtils {
	public static DialogUtils dialogUtils;

	public interface ItemSelect {
		void itemSelect(int pos);
	}
	public interface ReshActivity {
		void reshActivity();
	}
	public interface ReshHuihuanActivity {
		void reshActivity(int index);
	}

	public static synchronized DialogUtils getInstance() {
		if (dialogUtils == null) {
			dialogUtils = new DialogUtils();
		}
		return dialogUtils;
	}

	public void AlertDialogUtil(Context context, String message,
			final ReshActivity reshActivity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示：")
				.setMessage(message)
				.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								reshActivity.reshActivity();
							}
						}).setNegativeButton("取消", null);
		builder.create();
		builder.show();
		builder.setCancelable(false);
	}
	
	/**
	 * 拨打电话
	 * @param context
	 * @param tel
	 */
	public void CallDialog(final Context context, final String tel) {
//		AlertDialog.Builder builder = new AlertDialog.Builder(context);
//		builder.setTitle("提示：")
//				.setMessage("确定拨打："+tel+"？")
//				.setPositiveButton("确定",
//						new android.content.DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int which) {
//				//直接拨号				
		//Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ tel));
//					//调用拨号盘拨号
								Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+ tel)); 
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
								context.startActivity(intent);
//							}
//						})
//				.setNegativeButton("取消",
//						new android.content.DialogInterface.OnClickListener() {
//							@Override
//							public void onClick(DialogInterface dialog,
//									int which) {
//							}
//						});
//		builder.create();
//		builder.show();
//		builder.setCancelable(false);

	}
	
	
	
	/**
	 * 
	 * @param context
	 * @param message
	 * @param isNegative false、没有取消按钮
	 * @param reshActivity
	 */
	public void backDialog(final Context context,String message,boolean isNegative,final ReshActivity reshActivity){
		View view=LayoutInflater.from(context).inflate(R.layout.ep_alert_dialog, null);
		final Dialog dialog = new Dialog(context,R.style.alert_dialog);
		dialog.setContentView(view);
		((TextView)view.findViewById(R.id.message)).setText(message);
		Button positiveButton=(Button) view.findViewById(R.id.positiveButton);
		Button negativeButton=(Button) view.findViewById(R.id.negativeButton);
		View view_div = view.findViewById(R.id.view_div);
		if (!isNegative) {
			negativeButton.setVisibility(View.GONE);
			view_div.setVisibility(View.GONE);
		}
		positiveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (reshActivity!=null) {
					reshActivity.reshActivity();
				}
			}
		});
		negativeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	
	/**
	 * 
	 * @param context
	 * @param message
	 * @param isNegative false、没有取消按钮
	 * @param reshActivity
	 */
	public void backDialog1(final Context context,String message,boolean isNegative,final ReshActivity reshActivity){
		View view=LayoutInflater.from(context).inflate(R.layout.ep_alert_dialog, null);
		final Dialog dialog = new Dialog(context,R.style.alert_dialog);
		dialog.setContentView(view);
		((TextView)view.findViewById(R.id.message)).setText(message);
		Button positiveButton=(Button) view.findViewById(R.id.positiveButton);
		Button negativeButton=(Button) view.findViewById(R.id.negativeButton);
		positiveButton.setText("沟通");
		View view_div = view.findViewById(R.id.view_div);
		if (!isNegative) {
			negativeButton.setVisibility(View.GONE);
			view_div.setVisibility(View.GONE);
		}
		positiveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (reshActivity!=null) {
					reshActivity.reshActivity();
				}
			}
		});
		negativeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	public void listDialog(final Context context,ArrayList<HashMap<String, String>> list,final ItemSelect itemSelect){
		View view=LayoutInflater.from(context).inflate(R.layout.list_pop_view, null);
		final Dialog dialog = new Dialog(context,R.style.alert_dialog);
		
		ListView listview = (ListView)view.findViewById(R.id.pop_list);
		SimpleAdapter sim = new SimpleAdapter(context, list, android.R.layout.simple_list_item_1, new String[]{"city"}, new int[]{android.R.id.text1});

		listview.setAdapter(sim);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				itemSelect.itemSelect(position);
			}
		});
		dialog.setContentView(view);
		dialog.show();
	}
	public class ViewHolder {
		public TextView text;
	}
	
	public void duihuanDialog(final Context context,String message,final ReshHuihuanActivity reshActivity){
		View view=LayoutInflater.from(context).inflate(R.layout.ep_alert_dialog, null);
		final Dialog dialog = new Dialog(context,R.style.alert_dialog);
		dialog.setContentView(view);
		((TextView)view.findViewById(R.id.message)).setText(message);
		Button positiveButton=(Button) view.findViewById(R.id.positiveButton);
		Button negativeButton=(Button) view.findViewById(R.id.negativeButton);
		positiveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (reshActivity!=null) {
					reshActivity.reshActivity(0);
				}
			}
		});
		negativeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (reshActivity!=null) {
					reshActivity.reshActivity(1);
				}
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	
	
	
	
	

	public void RegistDialog(Context context, final ItemCall onItemCall) {
		final Dialog dialog = new Dialog(context, R.style.bottom_dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pop_view, null);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setWindowAnimations(R.style.bottom_dialog);
		dialogWindow.setGravity(Gravity.BOTTOM);
		view.findViewById(R.id.btn_txt_user).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(1);
					}
				});
		view.findViewById(R.id.btn_txt_shop).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(2);
					}
				});

		view.findViewById(R.id.txt_cancle).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
		dialog.show();
	}
	
	
	public void PulishHelpDialog(Context context, final ItemCall onItemCall) {
		final Dialog dialog = new Dialog(context, R.style.bottom_dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pop_view_publish_help, null);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setWindowAnimations(R.style.bottom_dialog);
		dialogWindow.setGravity(Gravity.BOTTOM);
		view.findViewById(R.id.btn_txt_xuanshang).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(0);
					}
				});
		view.findViewById(R.id.btn_txt_jishi).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(1);
					}
				});
		view.findViewById(R.id.btn_txt_huodong).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(2);
					}
				});

		view.findViewById(R.id.txt_cancle).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	public void SelectPicDialog(Context context, final ItemCall onItemCall) {
		final Dialog dialog = new Dialog(context, R.style.bottom_dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pop_select_pic, null);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setWindowAnimations(R.style.bottom_dialog);
		dialogWindow.setGravity(Gravity.BOTTOM);
		view.findViewById(R.id.btn_txt_paizhao).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(0);
					}
				});
		view.findViewById(R.id.btn_txt_xiangce).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(1);
					}
				});

		view.findViewById(R.id.txt_cancle).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
		dialog.show();
	}
	
	public void SelectSexDialog(Context context, final ItemCall onItemCall) {
		final Dialog dialog = new Dialog(context, R.style.bottom_dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pop_select_sex, null);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setWindowAnimations(R.style.bottom_dialog);
		dialogWindow.setGravity(Gravity.BOTTOM);
		view.findViewById(R.id.btn_txt_boy).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(0);
					}
				});
		view.findViewById(R.id.btn_txt_girl).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						onItemCall.onIndexCall(1);
					}
				});

		view.findViewById(R.id.txt_cancle).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
		dialog.show();
	}


	public interface ItemCall {
		void onIndexCall(int index);
	};

}
